home *** CD-ROM | disk | FTP | other *** search
- class CCollisionResolverBucketSort implements ICollisionResolver
- {
- var m_matrixWidth;
- var m_matrixHeight;
- var m_stageUpperLeftCorner;
- var m_stagePixelsPerMatrixWidth;
- var m_stagePixelsPerMatrixHeight;
- var m_bucketMatrix;
- var m_arrActiveBuckets;
- function CCollisionResolverBucketSort(width, height, stageWidth, stageHeight, stageLeft, stageTop)
- {
- this.m_matrixWidth = width;
- this.m_matrixHeight = height;
- this.m_stageUpperLeftCorner = new Vector2D(stageLeft,stageTop);
- this.m_stagePixelsPerMatrixWidth = stageWidth / this.m_matrixWidth;
- this.m_stagePixelsPerMatrixHeight = stageHeight / this.m_matrixHeight;
- this.m_bucketMatrix = new Array(this.m_matrixWidth * this.m_matrixHeight);
- var _loc3_ = 0;
- while(_loc3_ < this.m_matrixWidth)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.m_matrixHeight)
- {
- this.m_bucketMatrix[_loc3_ + _loc2_ * this.m_matrixWidth] = new CCollisionBucket(_loc3_,_loc2_);
- _loc2_ = _loc2_ + 1;
- }
- _loc3_ = _loc3_ + 1;
- }
- this.m_arrActiveBuckets = new Array();
- this.CreateDebugDisplay();
- }
- function UpdateStagePosition(stageLeft, stageTop, stageWidth, stageHeight)
- {
- this.m_stageUpperLeftCorner._x = stageLeft;
- this.m_stageUpperLeftCorner._y = stageTop;
- _global._bucketResolverDebugDisplay._x = stageLeft;
- _global._bucketResolverDebugDisplay._y = stageTop;
- }
- function get _matrixWidth()
- {
- return this.m_matrixWidth;
- }
- function get _matrixHeight()
- {
- return this.m_matrixHeight;
- }
- function CreateDebugDisplay()
- {
- if(!_global._bucketResolverDebugDisplay)
- {
- if(!_global._bucketResolverDebugDisplayRoot)
- {
- _global._bucketResolverDebugDisplayRoot = _root;
- }
- _global._bucketResolverDebugDisplay = _global._bucketResolverDebugDisplayRoot.createEmptyMovieClip("_bucketResolverDebugDisplay",_global._bucketResolverDebugDisplayRoot.getNextHighestDepth());
- }
- var _loc5_ = 0;
- while(_loc5_ < this.m_matrixWidth)
- {
- var _loc4_ = 0;
- while(_loc4_ < this.m_matrixHeight)
- {
- _global._bucketResolverDebugDisplay.attachMovie("Bucket Debug Square","_bucketDebug" + (_loc5_ + _loc4_ * this.m_matrixWidth),_global._bucketResolverDebugDisplay.getNextHighestDepth(),{_x:this.m_stageUpperLeftCorner._x + _loc5_ * this.m_stagePixelsPerMatrixWidth,_y:this.m_stageUpperLeftCorner._y + _loc4_ * this.m_stagePixelsPerMatrixHeight,_xscale:this.m_stagePixelsPerMatrixWidth,_yscale:this.m_stagePixelsPerMatrixHeight});
- _loc4_ = _loc4_ + 1;
- }
- _loc5_ = _loc5_ + 1;
- }
- }
- function GetMatrixCoordinatesFromLocation(loc)
- {
- return new Vector2D(int((loc._x - this.m_stageUpperLeftCorner._x) / this.m_stagePixelsPerMatrixWidth),int((loc._y - this.m_stageUpperLeftCorner._y) / this.m_stagePixelsPerMatrixHeight));
- }
- function ResolveCollisions(bodies)
- {
- this.m_arrActiveBuckets.splice(0);
- this.UpdateBodyBuckets(bodies);
- this.ResolveCollisionWithinBuckets();
- if(_global._bucketResolverDebugDisplay._visible)
- {
- this.UpdateBucketDebugDisplay();
- }
- }
- function UpdateBodyBuckets(bodies)
- {
- var _loc3_ = 0;
- while(_loc3_ < bodies.length)
- {
- var _loc2_ = bodies[_loc3_];
- if(_loc2_._doesCollide)
- {
- if(_loc2_._didMove || _loc2_._nUpdates == 1)
- {
- _loc2_.VacatePriorCollisionBuckets();
- this.AddBodyToOverlappingBuckets(_loc2_);
- }
- }
- else
- {
- _loc2_.VacatePriorCollisionBuckets();
- }
- _loc3_ = _loc3_ + 1;
- }
- }
- function UpdateBucketDebugDisplay()
- {
- var _loc6_ = 0;
- while(_loc6_ < this.m_matrixWidth)
- {
- var _loc3_ = 0;
- while(_loc3_ < this.m_matrixHeight)
- {
- var _loc8_ = _global._bucketResolverDebugDisplay["_bucketDebug" + (_loc6_ + _loc3_ * this.m_matrixWidth)];
- var _loc7_ = new Color(_loc8_);
- _loc7_.setRGB(0);
- var _loc4_ = this.m_bucketMatrix[_loc6_ + _loc3_ * this.m_matrixWidth];
- if(_loc4_.m_arrOverlappingBodies.length > 0)
- {
- var _loc5_ = 65280;
- if(_loc4_.m_arrOverlappingBodies.length > 1)
- {
- _loc5_ = 16711680;
- }
- _loc7_.setRGB(_loc5_);
- }
- _loc3_ = _loc3_ + 1;
- }
- _loc6_ = _loc6_ + 1;
- }
- }
- function AddBodyToOverlappingBuckets(body)
- {
- var _loc5_ = this.GetMatrixCoordinatesFromLocation(body._topLeftCorner);
- var _loc6_ = this.GetMatrixCoordinatesFromLocation(body._bottomRightCorner);
- var _loc4_ = _loc5_._x;
- while(_loc4_ <= _loc6_._x)
- {
- if(!(_loc4_ < 0 || _loc4_ >= this.m_matrixWidth))
- {
- var _loc2_ = _loc5_._y;
- while(_loc2_ <= _loc6_._y)
- {
- if(!(_loc2_ < 0 || _loc2_ >= this.m_matrixHeight))
- {
- var _loc3_ = this.m_bucketMatrix[int(_loc4_ + _loc2_ * this.m_matrixWidth)];
- _loc3_.AddBody(body);
- if(_loc3_.m_arrOverlappingBodies.length >= 2)
- {
- this.NoteActiveBucket(_loc3_);
- }
- }
- _loc2_ = _loc2_ + 1;
- }
- }
- _loc4_ = _loc4_ + 1;
- }
- }
- function NoteActiveBucket(bucket)
- {
- var _loc3_ = this.m_arrActiveBuckets.length;
- var _loc2_ = 0;
- while(_loc2_ < _loc3_)
- {
- if(this.m_arrActiveBuckets[_loc2_] == bucket)
- {
- return undefined;
- }
- _loc2_ = _loc2_ + 1;
- }
- this.m_arrActiveBuckets.push(bucket);
- }
- function ResolveCollisionWithinBuckets()
- {
- var _loc8_ = new Array();
- var _loc11_ = this.m_arrActiveBuckets.length;
- var _loc10_ = 0;
- while(_loc10_ < _loc11_)
- {
- var _loc5_ = this.m_arrActiveBuckets[_loc10_].m_arrOverlappingBodies;
- var _loc9_ = 0;
- while(_loc9_ < _loc5_.length - 1)
- {
- var _loc4_ = _loc5_[_loc9_];
- if(!_loc4_)
- {
- _loc5_.splice(_loc9_,1);
- _loc9_ = _loc9_ - 1;
- }
- else
- {
- var _loc2_ = _loc9_ + 1;
- while(_loc2_ < _loc5_.length)
- {
- var _loc3_ = _loc5_[_loc2_];
- if(!_loc3_)
- {
- _loc5_.splice(_loc2_,1);
- _loc2_ = _loc2_ - 1;
- }
- else
- {
- FreshDebug.Assert(_loc4_ != _loc3_,"CCollisionResolverBucketSort.ResolveCollisionWithinBuckets(): bodyI [ " + _loc4_ + " ] != bodyJ");
- if(!this.HasCollisionPair(_loc4_,_loc3_,_loc8_))
- {
- var _loc6_ = new Object();
- _loc6_._body1 = _loc4_;
- _loc6_._body2 = _loc3_;
- _loc8_.push(_loc6_);
- }
- }
- _loc2_ = _loc2_ + 1;
- }
- }
- _loc9_ = _loc9_ + 1;
- }
- _loc10_ = _loc10_ + 1;
- }
- _loc9_ = 0;
- while(_loc9_ < _loc8_.length)
- {
- var _loc7_ = _loc8_[_loc9_]._body1.DoesCollideWith(_loc8_[_loc9_]._body2);
- if(_loc7_)
- {
- if(!_loc7_.m_body1.DoesSuppressCentralizedCollisionResolution() && !_loc7_.m_body2.DoesSuppressCentralizedCollisionResolution())
- {
- this.ResolveCollision(_loc7_);
- }
- _loc7_.m_body1.NotifyCollision(_loc7_);
- _loc7_.m_body2.NotifyCollision(_loc7_);
- }
- _loc9_ = _loc9_ + 1;
- }
- }
- function HasCollisionPair(bodyI, bodyJ, arrPairs)
- {
- var _loc2_ = 0;
- while(_loc2_ < arrPairs.length)
- {
- var _loc1_ = arrPairs[_loc2_];
- if(_loc1_._body1 == bodyI && _loc1_._body2 == bodyJ || _loc1_._body1 == bodyJ && _loc1_._body2 == bodyI)
- {
- return true;
- }
- _loc2_ = _loc2_ + 1;
- }
- return false;
- }
- function ResolveCollision(collisionInfo)
- {
- var _loc2_ = collisionInfo.m_body1.m_restitution * collisionInfo.m_body2.m_restitution;
- var _loc6_ = collisionInfo.m_body1.m_mass / (collisionInfo.m_body1.m_mass + collisionInfo.m_body2.m_mass);
- var _loc13_ = collisionInfo.m_body2.m_mass / (collisionInfo.m_body1.m_mass + collisionInfo.m_body2.m_mass);
- var _loc5_ = collisionInfo.m_normal;
- var _loc8_ = _loc5_.GetInverse();
- var _loc10_ = collisionInfo.m_body1._velocity;
- var _loc9_ = collisionInfo.m_body2._velocity;
- var _loc3_ = _loc5_.GetMultiplyScalar(_loc5_.DotProduct(_loc10_));
- var _loc11_ = _loc10_.GetSubtract(_loc3_);
- var _loc4_ = _loc8_.GetMultiplyScalar(_loc8_.DotProduct(_loc9_));
- var _loc12_ = _loc9_.GetSubtract(_loc4_);
- var _loc7_ = _loc11_.GetSubtract(_loc3_.GetMultiplyScalar(_loc6_ * _loc2_).GetSubtract(_loc4_).GetMultiplyScalar(_loc13_ * _loc2_));
- if(collisionInfo.m_body2.m_mass <= 0)
- {
- _loc7_ = _loc11_.GetSubtract(_loc3_.GetMultiplyScalar(_loc2_));
- }
- var _loc14_ = _loc12_.GetSubtract(_loc4_.GetMultiplyScalar(_loc13_ * _loc2_).GetSubtract(_loc3_).GetMultiplyScalar(_loc6_ * _loc2_));
- if(collisionInfo.m_body1.m_mass <= 0)
- {
- _loc14_ = _loc12_.GetSubtract(_loc4_.GetMultiplyScalar(_loc2_));
- }
- collisionInfo.m_body1.ApplyImpulse(_loc7_);
- collisionInfo.m_body2.ApplyImpulse(_loc14_);
- }
- }
-